package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.cw.data.ConstantEnum;
import com.cw.data.Result;
import com.cw.pojo.TbGoods;
import com.cw.pojo.TbGoodsDesc;
import com.cw.pojo.TbItem;
import com.cw.pojo.TbSeller;
import com.cw.service.GoodService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsController")
public class GoodsController {

    @Reference
    private GoodService goodService;

    @RequestMapping("saveGoods")
    @ResponseBody
    public Result saveGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String propertys, String thp1, String thp2) {

        try {

            List<TbItem> itemList = JSONObject.parseArray(propertys, TbItem.class);

            tbGoods.setAuditStatus("0");

            tbGoods.setIsMarketable("1");

            Subject subject = SecurityUtils.getSubject();

            TbSeller ts = (TbSeller) subject.getPrincipal();

            tbGoods.setSellerId(ts.getSellerId());

            goodService.saveGoods(tbGoods, tbGoodsDesc, itemList, ts.getName(), thp1, thp2);

            return Result.result(ConstantEnum.SUCCESS);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

}
